package main

import (
	"context"
	"encoding/json"
	"git.900sui.cn/kc/base/common/toolLib"
	"git.900sui.cn/kc/base/utils"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/comment"
	"git.900sui.cn/kc/rpcinterface/client/staff"
	comment2 "git.900sui.cn/kc/rpcinterface/interface/comment"
	staff2 "git.900sui.cn/kc/rpcinterface/interface/staff"
	"github.com/streadway/amqp"
	"strconv"
)

/*
	员工服务评分任务统计：交换机=exchange.servicecomment
*/
func main() {
	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	new(utils.KcLog).GetInstance(kcgin.AppConfig.String("queue.log.servicecommentStaff"))
	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("queue.servicecommentStaff"), func(msg amqp.Delivery) {
		serviceCommentId, err := strconv.Atoi(string(msg.Body))
		if err != nil {
			utils.Logerr.Printf("评论id转换成int失败，serviceCommentId:%s, err:%s", string(msg.Body), err)
		}
		if serviceCommentId == 0 {
			utils.Logerr.Printf("无效的数据,serviceCommentId=:%v", serviceCommentId)
		}
		utils.Logerr.Printf("接受数据-serviceCommentId=:%v", serviceCommentId)
		//根据评价ID查找评价数据
		rpcComment := new(comment.ServiceComment).Init()
		defer rpcComment.Close()
		var serviceCommentRes comment2.ReplyGetServiceCommentScore
		if err := rpcComment.GetServiceCommentScoreById(context.Background(), &serviceCommentId, &serviceCommentRes); err != nil && toolLib.GetKcErrCode(err) != "30001" {
			utils.Logerr.Printf("根据评价ID获取评价得分错误,serviceCommentId:%d, error:%s", serviceCommentId, err.Error())
		}
		bytes, _ := json.Marshal(serviceCommentRes)
		utils.Logerr.Printf("评价serviceCommentId:%d，的得分数据信息为：%s", serviceCommentId, string(bytes))
		//根据用户评价的评分更新员工的评分
		if serviceCommentRes.Id > 0 && serviceCommentRes.StaffId > 0 {
			rpcStaffRank := new(staff.StaffRank).Init()
			defer rpcStaffRank.Close()
			var rpcStaffRamlRes bool
			if err := rpcStaffRank.AddStaffRankRpc(context.Background(), &staff2.StaffRankBase{
				StaffId:      serviceCommentRes.StaffId,
				ServiceScore: serviceCommentRes.ServiceScore,
			}, &rpcStaffRamlRes); err != nil {
				utils.Logerr.Printf("增加员工评价得分处理错误,评价serviceCommentId:%d, error:%s", serviceCommentRes.Id, err.Error())
			}
		}
		utils.Logerr.Printf("员工服务评分统计任务处理完成，serviceCommentId=%d", serviceCommentId)
	})
	if err != nil {
		utils.Logerr.Printf("员工服务评分统计任务处理错误：%s", err.Error())
	}
}
